package BZimmerman.AndroidWars;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.view.View;

public class initializeGraphics {
	//Tiles
	public GraphicObject floor,wall,water,mountain;
	//Utilites
	public GraphicObject select;
	public GraphicObject arrow_body;
	public GraphicObject arrow_curve;
	public GraphicObject arrow_end;
	//Heros
	public GraphicObject class_fighter;
	//Palaces
	public GraphicObject palace_survivors01,palace_survivors02,palace_survivors03,palace_survivors04,palace_survivors05;
	private Bitmap[] tiles,utilities,heroes;
	private Bitmap[][] palaces;
	private int scale;
	
	
	initializeGraphics(Resources resources, int scale){
		this.scale = scale;
		floor = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.dirt_basic));
	    wall = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.wall_basic));
	    water = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.wall_water));
	    mountain = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.wall_mountain));
	    select = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.selector));
	    arrow_body = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.arrow_body));
	    arrow_curve = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.arrow_curve));
	    arrow_end = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.arrow_end));
	    class_fighter = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.class_herofighter));
	    palace_survivors01 = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.palace_survivors01));
	    palace_survivors02 = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.palace_survivors02));
	    palace_survivors03 = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.palace_survivors03));
	    palace_survivors04 = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.palace_survivors04));
	    palace_survivors05 = new GraphicObject(BitmapFactory.decodeResource(resources, R.drawable.palace_survivors05));
	    //0
		Bitmap[] tileHolder = {wall.getGraphic(), floor.getGraphic(), water.getGraphic(), mountain.getGraphic()};
		//1
		Bitmap[] utilityHolder = {select.getGraphic(),arrow_body.getGraphic(),arrow_curve.getGraphic(),arrow_end.getGraphic()};
		//2
		Bitmap[] heroesHolder = {class_fighter.getGraphic()};
		Bitmap[] survivorPalaceHolder = {palace_survivors01.getGraphic(),palace_survivors02.getGraphic(),
				palace_survivors03.getGraphic(),palace_survivors04.getGraphic(),palace_survivors05.getGraphic()};
		//End
		tiles = new Bitmap[tileHolder.length];
		utilities = new Bitmap[utilityHolder.length];
		heroes = new Bitmap[heroesHolder.length];
		palaces = new Bitmap[5][5];
		int count = 0; for(Bitmap item :scaleImages(scale,tileHolder,wall)){tiles[count]=item;count++;}
	    count = 0; for(Bitmap item :scaleImages(scale,utilityHolder,select)){utilities[count]=item;count++;}
	    count = 0; for(Bitmap item :scaleImages(scale,heroesHolder,select)){heroes[count]=item;count++;}
	    count = 0; for(Bitmap item :scaleImages(scale,survivorPalaceHolder,select)){palaces[0][count]=item;count++;}
	}
	
	public Bitmap rotateImage(GraphicObject image, int degrees, Bitmap sample, boolean flip){
		Matrix tempMatrix = new Matrix();
		//This scales the image down to the correct size
		tempMatrix = image.returnMatrix(sample.getWidth(),sample.getHeight(),scale,false);
		if(flip){
			//Flip over Y axis
			tempMatrix.preScale(1.0f, -1.0f);
		}
		//This sets the rotation
		tempMatrix.postRotate(degrees);
		//This rotates the image
		sample = Bitmap.createBitmap(sample, 0, 0,sample.getWidth(), sample.getHeight(), tempMatrix, true);
		//returns rotated image
		return sample;
	}
	
	public Bitmap[] scaleImages(int imageScale, Bitmap[] preConverted, GraphicObject sample){
		Bitmap[] converted = new Bitmap[preConverted.length];
    	
    	for(int i = 0;i<converted.length;i++){
    		converted[i] = Bitmap.createBitmap(preConverted[i], 0, 0,(preConverted[i]).getWidth(), 
        			(preConverted[i]).getHeight(), sample.returnMatrix((preConverted[i]).getWidth(),
        					(preConverted[i]).getHeight(),imageScale,false), true);
        }
    	return converted;
	}
	public Bitmap returnPalaces(int type, int location){
		return palaces[type][location];
	}
	
	public Bitmap returnTiles(int set, int location){
		switch(set){
			case 0: return tiles[location];
			case 1: return utilities[location];
			case 2: return heroes[location];
		}
		return tiles[0];
	}
	
	public int returnTileSizes(int set){
		switch(set){
			case 0: return tiles.length;
			case 1: return utilities.length;
			case 2: return heroes.length;
		}
		return tiles.length;
	}
	
	class GraphicObject {
        /**
         * Contains the coordinates of the graphic.
         */
        
        public class Coordinates {
            public int _x;
            public int _y;

            public int getX() {
                return _x;
            }
 
            public void setX(int value) {
            	_x = value;
            }
 
            public int getY() {
                return _y;
            }
 
            public void setY(int value) {
                _y = value;
            }
            
            public String toString() {
                return "Coordinates: (" + _x + "/" + _y + ")";
            }
        }
 
        private Bitmap _bitmap;
        private Coordinates _coordinates;
 
        public GraphicObject(Bitmap bitmap) {
            _bitmap = bitmap;
            _coordinates = new Coordinates();
        }
 
        public Bitmap getGraphic() {
            return _bitmap;
        }
 
        public Coordinates getCoordinates() {
            return _coordinates;
        }
        
        public Matrix returnMatrix(int width, int height, int scale, boolean expand){

        	float scaleWidth;
        	float scaleHeight;
        	
        	if(!expand){
        		scaleWidth = ((float) scale) / width;
        		scaleHeight = ((float) scale) / height;
        	}else{
        		scaleWidth = scale * width;
        		scaleHeight = scale * height;
        	}
        	Matrix matrix = new Matrix();
        	matrix.postScale(scaleWidth, scaleHeight);
        	
        	return matrix;
        }
        
    }
}
